﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

public partial class Visitor_ChangeCustomerProfile : System.Web.UI.Page
{
    string user;
    int AID;
    SqlConnection cnn;
    SqlCommand cmd;
    SqlDataReader dread;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["Username"] == null)
        {
            Response.Redirect("LoginFail.aspx");
        }
        user = Session["Username"].ToString();

        AID = Int32.Parse(Request.QueryString["AID"].ToString());
        cnn = new SqlConnection(ConfigurationManager.ConnectionStrings["OPOConnectionString"].ToString());
        cnn.Open();
        cmd = new SqlCommand("Select * from Customer where AccID = '" + AID + "'", cnn);
        dread = cmd.ExecuteReader();
        if (!IsPostBack)
        {
            if (dread.Read())
            {
                if (dread.GetString(5) == "Male")
                {
                    rdlGender.SelectedIndex = 0;
                }
                else
                rdlGender.SelectedIndex = 1;
                txtYName.Text = dread.GetString(2);
                bdpYBirthday.SelectedDate = DateTime.Parse(dread.GetString(4));
                txtYAddress.Text = dread.GetString(6);
                txtYPhone.Text = dread.GetString(7);
            }
        }
    }

    protected void Page_PreInit(object sender, EventArgs e)
    {
        if (Convert.ToString(Session["Username"]) == "")
        {
            this.Page.MasterPageFile = "~/MasterPage/Visitor.master";
        }
        else
        {
            this.Page.MasterPageFile = "~/MasterPage/VisitorAfterLogin.master";
        }
    }

    protected void imgbChange_Click(object sender, ImageClickEventArgs e)
    {
        dread.Close();
        cmd = new SqlCommand("Update Customer Set Cust_Name = @Cust_Name, Cust_Birthday = @Cust_Birthday, Cust_Address = @Cust_Address, Cust_Phone = @Cust_Phone, "
                            + "Cust_Gender = @Cust_Gender where AccID= '" + AID + "'", cnn);

        cmd.Parameters.Add(new SqlParameter("@Cust_Name", SqlDbType.NVarChar, 100));
        cmd.Parameters["@Cust_Name"].Value = txtYName.Text;
        cmd.Parameters.Add(new SqlParameter("@Cust_Birthday", SqlDbType.VarChar, 20));
        cmd.Parameters["@Cust_Birthday"].Value = bdpYBirthday.SelectedDate.ToString("M/d/yyyy");
        cmd.Parameters.Add(new SqlParameter("@Cust_Address", SqlDbType.NVarChar, 1000));
        cmd.Parameters["@Cust_Address"].Value = txtYAddress.Text;
        cmd.Parameters.Add(new SqlParameter("@Cust_Phone", SqlDbType.NVarChar, 20));
        cmd.Parameters["@Cust_Phone"].Value = txtYPhone.Text;
        cmd.Parameters.Add(new SqlParameter("@Cust_Gender", SqlDbType.NVarChar, 10));
        cmd.Parameters["@Cust_Gender"].Value = rdlGender.SelectedItem.ToString();
        cmd.ExecuteNonQuery();

        ScriptManager.RegisterStartupScript(this, this.GetType(), "key", "ShowMessage()", true);
    }

    protected void imgbCancel_Click(object sender, ImageClickEventArgs e)
    {
        Response.Redirect("index.aspx");
    }
}